package com.tiancikeji.zaoke.app.dao.order.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.tiancikeji.zaoke.app.dao.order.BillDao;
import com.tiancikeji.zaoke.app.model.order.Bill;

public class BillDaoImpl extends JdbcDaoSupport implements BillDao{
	
	private static final String TABLE_BILL = "`zaoke`.`bill`";

	private static final String QUERY_VIEW = "`id`, `money`, `user_id`, `code`, `trace`, `subject`, `body`, `out_serial_id`, `serial_id`, `time`, `return_url`, `status`, `back_url`, `pay_mode`";

	public long insert(final Bill bill) {
		final String sql = "insert into " + TABLE_BILL + " (`money`, `user_id`, `code`, `trace`, `subject`, `body`, `out_serial_id`, `serial_id`, `return_url`, `status`, `back_url`, `pay_mode`) values(?, ?, ?,?, ?, ?, ?,?, ?, ?, ?,?)";
		KeyHolder holder = new GeneratedKeyHolder();
		int count = getJdbcTemplate().update(new PreparedStatementCreator() {
			
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement ps = con.prepareStatement(sql);
				ps.setObject(1, bill.getMoney());
				ps.setObject(2, bill.getUserId());
				ps.setObject(3, bill.getCode());
				ps.setObject(4, bill.getTrace());
				ps.setObject(5, bill.getSubject());
				ps.setObject(6, bill.getBody());
				ps.setObject(7, bill.getOutSerialId());
				ps.setObject(8, bill.getSerialId());
				ps.setObject(9, bill.getReturnUrl());
				ps.setObject(10, bill.getStatus());
				ps.setObject(11, bill.getBackUrl());
				ps.setObject(12, bill.getPaymode());
				return ps;
			}
		}, holder);
		
		if(count > 0 && holder.getKey() != null)
			return holder.getKey().longValue();
		
		return 0;
	}

	public int updateStatus(long id, int statusSuccess) {
		String sql = "update " + TABLE_BILL + " set status = ? where id = ?";
		return getJdbcTemplate().update(sql, new Object[]{statusSuccess, id});
	}

	public int revertStatus(long id, int statusUnpaid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Bill getBySerialId(final String serialId) {
		String sql = "select " + QUERY_VIEW + " from " + TABLE_BILL + " where serial_id = ?";
		List<Bill> bills = getJdbcTemplate().query(sql, new Object[]{serialId}, new RowMapper<Bill>(){

			public Bill mapRow(ResultSet rs, int rowNum) throws SQLException {
				Bill bill = new Bill();
				bill.setBackUrl(rs.getString("back_url"));
				bill.setBody(rs.getString("body"));
				bill.setCode(rs.getString("code"));
				bill.setId(rs.getLong("id"));
				bill.setMoney(rs.getDouble("money"));
				bill.setOutSerialId(rs.getString("out_serial_id"));
				bill.setPaymode(rs.getInt("pay_mode"));
				bill.setReturnUrl(rs.getString("return_url"));
				bill.setSerialId(rs.getString("serial_id"));
				bill.setStatus(rs.getInt("status"));
				bill.setSubject(rs.getString("subject"));
				bill.setTime(rs.getDate("time"));
				bill.setTrace(rs.getString("trace"));
				bill.setUpdateTime(rs.getDate("time"));
				bill.setUserId(rs.getLong("user_id"));
				return bill;
			}
			
		});
		
		if(bills != null && bills.size() > 0)
			return bills.get(0);
		
		return null;
	}

	public List<Bill> getByOutSerialId(String outSerialId) {
		// TODO Auto-generated method stub
		return null;
	}

	public Bill getSuccessBillByOutSerialId(String outSerial) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Bill> findByUserId(long userId, int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getCountByUserId(long userId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public String getPayUrlByOutSerialId(String orderSerial) {
		// TODO Auto-generated method stub
		return null;
	}

}
